/**
 * 使用方法
 * <footer current="2"></footer>
 * */

const app = getApp();
Component({
    properties: {
        /**
         * 是否显示
         * */
        showNav: {type: Boolean},
        
        //动画
        animationDatas: {
            type: Object,
            value: false
        },
        // 订单价格
        price: {type: String, value: 0},
        
        // 余额
        money: {type: String},
        
        // 订单id
        orderId: {type: String},

        //优惠券信息
        selectedCoupon: {
            type: Object,
        },
        
    },
    data: {

        // 当前选中项目
        curryValue: 0,
        showSusses: false,
        couponList: [],
        selectedCoupon: null

    },

    created(){

        // 判断获取优惠劵列表
        this.loaderCouponList();
        
        // 初始化数据
        this.clearSelected();

    },

    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: function () { 
            console.log('run')
        },
      },

    methods: {

            

        // 清除选中缓存
        clearSelected(){
            app.globalData.selectedCoupon = null;
        },

        toCoupon(){
            app.navigateTo(app.page.Coupon)
        },

        toComplaintList(){
            app.navigateTo(app.page.ComplaintList)
        },

        getCoupn(){
            
            this.setData({
                selectedCoupon: this.data.selectedCoupon
            })
            
        },

        loaderCouponList(status=0){
            let _this = this;

            return app.request({
                
                url: app.api.url.user.couponList,
                pageSize: 999,
                status: status

            }).then(res =>{

                if(res.status == 1){
                    _this.setData({
                        couponList: res.data
                    })  
                }else{
                    return Promise.reject();
                }
                    
            }).catch(res =>{
                _this.showToast(res.msg)
            })
        },

        closed: function () {
            var that = this;
            var animation = wx.createAnimation({
                duration: 400,
                timingFunction: 'linear'
            })

            that.animation = animation
            animation.translateY(690).step()
            
            that.setData({
                animationDatas: animation.export(),
            })
            
            setTimeout(function () {
                animation.translateY(0).step()
                that.setData({
                    animationDatas: animation.export(),
                    showNav: false
                })
            }, 300)

        },
        
        onChange: function (e) {
            let $dataset = e.currentTarget.dataset;
            this.setData({
                curryValue: $dataset.type
            })
        },

        pays: function () {
            var _this = this;
            var orderId = _this.properties.orderId;
            var pay_type = _this.data.curryValue;
            var object = {
                url: app.api.url.order.orderPay,
                data: {order_id: orderId, pay_type: pay_type},
            }

            app.request(object).then(res =>{

                if(res.status == 1) {
                    _this.setData({
                        showSusses: true,
                        showNav: false
                    })
                    
                    _this.triggerEvent('myevent', _this.data.showSusses)
                    
                } else {

                    app.showToast(res.info);
                    
                }
                
            });

        }
    },
});
